/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.example.omsdkdemo.manager;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.device.idea.om.controller.DeviceController;
import com.device.idea.om.controller.OmServiceController;
import com.device.idea.om.controller.PackageController;
import com.device.idea.om.controller.TimeController;
import com.device.idea.om.controller.model.AppInfo;
import com.device.idea.om.controller.model.AppUsageStats;
import com.device.idea.om.controller.model.NtpInfo;
import com.device.idea.om.controller.model.StatPeriod;
import com.device.idea.om.controller.model.StorageUsageStats;
import com.example.omsdkdemo.utils.OmSdkDemoUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 功能描述
 *
 * @since 2023-02-27
 */
public class OmSdkManager {
    private static final String TAG = "OmSdkManager";

    private static final String WHITEBOARD_PACKAGE = "com.device.idea.demo";

    private static volatile OmSdkManager instance;

    private static final int MILLIS_PER_SECOND = 1000;

    private static final int SLEEP_TIMER_DELAY = 5;

    private Context context;

    private OmServiceController omServiceController;

    private PackageController packageController;

    private DeviceController deviceController;

    private TimeController timeController;

    private OmSdkManager(Context context) {
        this.context = context;
    }

    /**
     * 获取单例实例
     *
     * @return 单例
     */
    public static OmSdkManager getInstance(@NonNull Context context) {
        if (instance == null) {
            synchronized (OmSdkManager.class) {
                if (instance == null) {
                    instance = new OmSdkManager(context);
                }
            }
        }
        return instance;
    }

    public void destroy() {
        Log.i(TAG, this + " onDestroy");
        if (omServiceController != null) {
            omServiceController.destroy();
            omServiceController = null;
            deviceController = null;
            packageController = null;
            timeController = null;
        }
    }

    public Context getContext() {
        return context;
    }

    public void initController() {
        String filePath = context.getFilesDir().getPath();
        AppInfo appInfo = new AppInfo();
        appInfo.setAppId(context.getPackageName());
        appInfo.setPath(filePath);
        omServiceController = OmServiceController.create(context, appInfo);
        deviceController = omServiceController.getDeviceController();
        packageController = omServiceController.getPackageController();
        timeController = omServiceController.getTimeController();
    }

    public void getPlatformVersion() {
        String ret = omServiceController.getPlatformVersion();
        Toast.makeText(context, OmSdkDemoUtil.resultLog(ret), Toast.LENGTH_SHORT).show();
    }

    public void getSdkVersion() {
        String ret = omServiceController.getSdkVersion();
        Toast.makeText(context, OmSdkDemoUtil.resultLog(ret), Toast.LENGTH_SHORT).show();
    }

    public void setMicSwitch() {
        deviceController.setMicSwitch();
        Toast.makeText(context, OmSdkDemoUtil.resultLog(), Toast.LENGTH_SHORT).show();
    }

    public void getSpeakerVolume() {
        int ret = deviceController.getSpeakerVolume();
        Toast.makeText(context, OmSdkDemoUtil.resultLog(String.valueOf(ret)), Toast.LENGTH_SHORT).show();
    }

    public void sleep() {
        int ret = deviceController.sleep();
        Toast.makeText(context, OmSdkDemoUtil.resultLog(ret), Toast.LENGTH_SHORT).show();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                wakeup();
            }
        };
        Timer timer = new Timer();
        timer.schedule(task, SLEEP_TIMER_DELAY * MILLIS_PER_SECOND);
    }

    private void wakeup() {
        int ret = deviceController.wakeup();
        OmSdkDemoUtil.resultLog(ret);
    }

    public void reboot() {
        int ret = deviceController.reboot();
        Toast.makeText(context, OmSdkDemoUtil.resultLog(ret), Toast.LENGTH_SHORT).show();
    }

    public void shutdown() {
        int ret = deviceController.shutdown();
        Toast.makeText(context, OmSdkDemoUtil.resultLog(ret), Toast.LENGTH_SHORT).show();
    }

    public void setSpeakerVolume(String string) {
        int value;
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(string);
        if (!isNum.matches()) {
            value = 0;
        } else {
            value = Integer.parseInt(string);
        }
        int ret = deviceController.setSpeakerVolume(value);
        Toast.makeText(context, OmSdkDemoUtil.resultLog(ret), Toast.LENGTH_SHORT).show();
    }

    public StorageUsageStats getStorageUsageStats() {
        StorageUsageStats ret = deviceController.getStorageUsageStats();
        Toast
            .makeText(context,
                OmSdkDemoUtil.resultLog(String.format(Locale.getDefault(), "%d/%d", ret.getUsed(), ret.getTotal())),
                Toast.LENGTH_SHORT)
            .show();
        return ret;
    }

    public void showSystemStatusBar(long status) {
        packageController.showSystemStatusBar(status);
        Toast.makeText(context, OmSdkDemoUtil.resultLog(), Toast.LENGTH_SHORT).show();
    }

    public void restoreDefaultLauncher() {
        packageController.restoreDefaultLauncher();
        Toast.makeText(context, OmSdkDemoUtil.resultLog(), Toast.LENGTH_SHORT).show();
    }

    public void setDefaultLauncher() {
        packageController.setDefaultLauncher(context.getPackageName(), "com.example.omsdkdemo.OmDemoActivity");
        Toast.makeText(context, OmSdkDemoUtil.resultLog(), Toast.LENGTH_SHORT).show();
    }

    public void uninstallPackage() {
        int ret = packageController.uninstallPackage("com.example.androidtest", false);
        Toast.makeText(context, OmSdkDemoUtil.resultLog(ret), Toast.LENGTH_SHORT).show();
    }

    public void installPackage() {
        int ret = packageController.installPackage("/sdcard/Download/app-debug.apk",
            (packageName, result) -> Log.i(TAG, "result is " + result + " packageName is " + packageName));
        Toast.makeText(context, OmSdkDemoUtil.resultLog(ret), Toast.LENGTH_SHORT).show();
    }

    public String getSignature() {
        String packageName = context.getPackageName();
        String ret = packageController.getSignature(packageName);
        Toast.makeText(context, OmSdkDemoUtil.resultLog(ret), Toast.LENGTH_SHORT).show();
        return ret;
    }

    public List<AppUsageStats> getAppUsageStats(StatPeriod statPeriod) {
        List<AppUsageStats> ret = packageController.getAppUsageStats(statPeriod);
        Toast.makeText(context, OmSdkDemoUtil.resultLog(), Toast.LENGTH_SHORT).show();
        return ret;
    }

    public void clearPackageData() {
        packageController.clearPackageData(WHITEBOARD_PACKAGE);
        Toast.makeText(context, OmSdkDemoUtil.resultLog(), Toast.LENGTH_SHORT).show();
    }

    public void getDisallowedUninstallPackageList() {
        List<String> packageNames = packageController.getDisallowedUninstallPackageList();
        StringBuilder stringBuilder = new StringBuilder();
        for (String packageName : packageNames) {
            stringBuilder.append(packageName);
        }
        Toast.makeText(context, OmSdkDemoUtil.resultLog(stringBuilder.toString()), Toast.LENGTH_SHORT).show();
    }

    public void removeDisallowedUninstallPackages() {
        List<String> packageNames = new ArrayList<>();
        packageNames.add(WHITEBOARD_PACKAGE);
        packageController.removeDisallowedUninstallPackages(packageNames);
        Toast.makeText(context, OmSdkDemoUtil.resultLog(), Toast.LENGTH_SHORT).show();
    }

    public void addDisallowedUninstallPackages() {
        List<String> packageNames = new ArrayList<>();
        packageNames.add(WHITEBOARD_PACKAGE);
        packageController.addDisallowedUninstallPackages(packageNames);
        Toast.makeText(context, OmSdkDemoUtil.resultLog(), Toast.LENGTH_SHORT).show();
    }

    public void clearSingleApp() {
        String packageName = context.getPackageName();
        int isSuccess = packageController.clearSingleApp(packageName);
        Toast.makeText(context, OmSdkDemoUtil.resultLog(isSuccess), Toast.LENGTH_SHORT).show();
    }

    public void addSingleApp() {
        String packageName = context.getPackageName();
        int isSuccess = packageController.addSingleApp(packageName);
        Toast.makeText(context, OmSdkDemoUtil.resultLog(isSuccess), Toast.LENGTH_SHORT).show();
    }

    public void getSingleApp() {
        String ret = packageController.getSingleApp();
        Toast.makeText(context, OmSdkDemoUtil.resultLog(ret), Toast.LENGTH_SHORT).show();
    }

    public void removePersistentApp() {
        List<String> packageNames = new ArrayList<>();
        packageNames.add(context.getPackageName());
        packageController.removePersistentApp(packageNames);
        Toast.makeText(context, OmSdkDemoUtil.resultLog(), Toast.LENGTH_SHORT).show();
    }

    public void addPersistentApp() {
        List<String> packageNames = new ArrayList<>();
        packageNames.add(context.getPackageName());
        packageController.addPersistentApp(packageNames);
        Toast.makeText(context, OmSdkDemoUtil.resultLog(), Toast.LENGTH_SHORT).show();
    }

    public void getPersistentApp() {
        List<String> result = packageController.getPersistentApp();
        StringBuilder stringBuilder = new StringBuilder();
        for (String ret : result) {
            stringBuilder.append(ret);
        }
        Toast.makeText(context, OmSdkDemoUtil.resultLog(stringBuilder.toString()), Toast.LENGTH_SHORT).show();
    }

    public void getNtpInfo() {
        NtpInfo ntpInfo = timeController.getNtpInfo();
        Toast.makeText(context, OmSdkDemoUtil.resultLog(ntpInfo.getServerAddress()), Toast.LENGTH_SHORT).show();
    }

    public void setNtpInfo(String string) {
        NtpInfo ntpInfo = new NtpInfo();
        ntpInfo.setServerAddress(string);
        int ret = timeController.setNtpInfo(ntpInfo);
        Toast.makeText(context, OmSdkDemoUtil.resultLog(ret), Toast.LENGTH_SHORT).show();
    }
}
